﻿using BW.WMS.Ml.net;
using BW.WMS.Ml.net.BW.WMS.Ml.net;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;

namespace BW.WMS.Web.Controllers
{
    [Route("api/stock-prediction")]
    public class StockPredictionController : AbpController
    {
        /// <summary>
        /// 获取指定时间段的入库数量预测
        /// </summary>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns>日期、历史数据和预测数据的列表</returns>
        [HttpGet("predict")]
        public ActionResult<List<Prediction.StockPredictionResult>> PredictStockData(
            [FromQuery] DateTime startDate,
            [FromQuery] DateTime endDate)
        {
            try
            {
                if (startDate > endDate)
                {
                    return BadRequest("开始日期不能晚于结束日期");
                }

                // 这里可以从数据库获取历史数据
                // 作为示例，我们创建一些模拟的历史数据
                var historicalData = GetHistoricalData(startDate, endDate);

                // 调用预测服务
                var predictions = Prediction.PredictDateRange(startDate, endDate, historicalData);

                return Ok(predictions);
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"预测过程发生错误: {ex.Message}");
            }
        }

        /// <summary>
        /// 带自定义历史数据的预测接口
        /// </summary>
        [HttpPost("predict-with-data")]
        public ActionResult<List<Prediction.StockPredictionResult>> PredictWithCustomData(
            [FromBody] PredictionRequest request)
        {
            try
            {
                if (request.StartDate > request.EndDate)
                {
                    return BadRequest("开始日期不能晚于结束日期");
                }

                Dictionary<DateTime, float> historicalData = null;

                if (request.HistoricalData != null && request.HistoricalData.Count > 0)
                {
                    historicalData = new Dictionary<DateTime, float>();
                    foreach (var item in request.HistoricalData)
                    {
                        historicalData[item.Date] = item.Value;
                    }
                }

                var predictions = Prediction.PredictDateRange(request.StartDate, request.EndDate, historicalData);

                return Ok(predictions);
            }
            catch (Exception ex)
            {
                return StatusCode(500, $"预测过程发生错误: {ex.Message}");
            }
        }

        /// <summary>
        /// 获取历史数据（示例方法，实际应用中应从数据库获取）
        /// </summary>
        private Dictionary<DateTime, float> GetHistoricalData(DateTime startDate, DateTime endDate)
        {
            // 这里应该是从数据库获取数据
            // 作为示例，我们创建一些模拟数据
            var data = new Dictionary<DateTime, float>();

            // 创建一些测试数据，实际项目中应替换为从数据库查询
            var random = new Random();
            var currentDate = startDate;

            while (currentDate <= endDate)
            {
                // 只为部分日期提供历史数据，模拟真实情况
                if (random.Next(0, 2) == 1)
                {
                    data[currentDate] = random.Next(50, 200);
                }

                currentDate = currentDate.AddDays(1);
            }

            return data;
        }
    }

    /// <summary>
    /// 预测请求模型
    /// </summary>
    public class PredictionRequest
    {
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public List<HistoricalDataItem> HistoricalData { get; set; }
    }

    /// <summary>
    /// 历史数据项
    /// </summary>
    public class HistoricalDataItem
    {
        public DateTime Date { get; set; }
        public float Value { get; set; }
    }
}